<?php
defined('IN_APPS') or die('No direct access!');

// load main controller 
require_once INCLUDE_PATH . DS . 'controller.php';

// load main model
require_once INCLUDE_PATH . DS . 'model.php';

// load Smarty class
require_once LIBRARY_PATH . DS . 'smarty' . DS . 'Smarty.class.php';

// load Security class
require_once LIBRARY_PATH . DS . 'ci' . DS . 'Security.php';


session_start();



// ----------------------------------------------------------------------------
// PHP-IDS
// ----------------------------------------------------------------------------
// 
// set the include path properly for PHPIDS
set_include_path(
    get_include_path()
    . PATH_SEPARATOR
    . LIBRARY_PATH . DS . 'php-ids' . DS . 'lib' . DS
);

require_once LIBRARY_PATH . DS . 'php-ids' . DS . 'lib' . DS . 'IDS' . DS . 'Init.php';

try {
    $request = array(
		'REQUEST'	=> $_REQUEST,
		'GET'		=> $_GET,
		'POST'		=> $_POST,
		'COOKIE'	=> $_COOKIE
    );
	
	// load PHP-IDS config file
    $init = IDS_Init::init( LIBRARY_PATH . DS . 'php-ids' . DS . 'lib' . DS . 'Config' . DS . 'Config.ini');
	
    $init->config['General']['base_path'] = LIBRARY_PATH . DS . 'php-ids' . DS;
    $init->config['General']['use_base_path'] = true;
    $init->config['Caching']['caching'] = 'file';
	
    $ids = new IDS_Monitor($request, $init);
    $result = $ids->run();
    if (!$result->isEmpty()) {
        require_once 'IDS' . 'Log' . 'File.php';
        require_once 'IDS' . 'Log' . 'Email.php';
        require_once 'IDS' . 'Log' . 'Composite.php';
		
        $compositeLog = new IDS_Log_Composite();
        $compositeLog->addLogger(IDS_Log_Email::getInstance($init),IDS_Log_File::getInstance($init));
        $compositeLog->execute($result);
    }
} 
catch (Exception $e) {
   //this shouldn't happen and if it does you don't want the notification public.
}
// ----------------------------------------------------------------------------
// PHP-IDS
// ----------------------------------------------------------------------------



// get query string so we can set which controller to load
$uri = $_SERVER['QUERY_STRING'];

// using parse_str, we can form an array of url segment
parse_str($uri, $segment);

// default variable to hold controller's name
$controller = (empty($segment['controller']) OR !isset($segment['controller'])) ? 'frontpage' : $segment['controller'];

// unset the $segment['controller'] because we don't need it anymore
unset($segment['controller']);

// default variable to hold controller's method name
$action = (empty($segment['action']) OR !isset($segment['action'])) ? 'index' : $segment['action'];

// unset the $segment['action'] because we don't need it anymore
unset($segment['action']);




// check if we have $segment['message'] in url query string
unset($segment['message']);

// check if we have $segment['type'] in url query string
unset($segment['type']);

unset($segment['PHPSESSID']);


// now try check if the file really exists
if (file_exists(CONTROLLER_PATH . DS . $controller . '.php')) {

	// yes, file exists. now load it to our apps
	require_once CONTROLLER_PATH . DS . $controller . '.php';
	
	// now we change lette case using ucfirst
	$controller = ucfirst($controller); 
}
// file doesn't exists
else {
	// exit apps with some notice
	die('Error loading ' . $controller . ' file!');
}

// default variable to hold other parameters
$params = '';

// check if the $segment is not empty
if (!empty($segment)) {
	$params = implode(', ', $segment);
}


// check if the $segment is not empty
//if (!empty($segment)) {
//	
//	foreach($segment as $key => $value) {
//		$params[$key] = $value;
//	}
//}

// we will use $view instead of $smarty to respect our MVC style :)
$view = new Smarty();

// set some default properties for Smarty
$view->setTemplateDir(VIEW_PATH);
$view->setCompileDir(BASE_PATH . DS . 'cache' . DS . 'views');
$view->setCacheDir(BASE_PATH . DS . 'cache');